package com.rc.saas.tenant.service.shop;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.shop.ShopGoodsMapper;
import com.rc.saas.tenant.mapper.shop.ShopOrderGoodsMapper;
import com.rc.saas.tenant.mapper.shop.ShopOrderMapper;
import com.rc.saas.tenant.mapper.shop.ShopStockRecordMapper;
import com.rc.saas.tenant.model.shop.*;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.shop.ShopOrderSearch;
import com.rc.saas.tenant.search.shop.ShopStockRecordSearch;
import com.rc.saas.tenant.vo.shop.ShopOrderHistoryVo;
import com.rc.saas.tenant.vo.shop.ShopOrderResultVo;
import com.rc.saas.tenant.vo.shop.ShopOrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class ShopOrderServiceImpl implements ShopOrderService {
    @Autowired
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopOrderGoodsMapper shopOrderGoodsMapper;

    @Autowired
    private ShopGoodsMapper shopGoodsMapper;

    @Autowired
    private ShopStockRecordMapper shopStockRecordMapper;

    @Override
    public ShopOrderMapper getMapper() {
        return shopOrderMapper;
    }


    @Override
    public PageInfo<ShopOrderResultVo> listPageByCondition(ShopOrderSearch shopOrderSearch) {
        PageHelper.startPage(shopOrderSearch.getOffset(), shopOrderSearch.getLimit());
        List<ShopOrderResultVo> shopOrderResultVos = shopOrderMapper.listByCondition(shopOrderSearch);
        return new PageInfo<ShopOrderResultVo>(shopOrderResultVos);
    }

    @Override
    public Long insertReturnId(ShopOrder shopOrder) {
        return shopOrderMapper.insertReturnId(shopOrder);
    }


    @Override
    public ShopOrderStatisticsVo selectShopOrderStatistics(String tenantCode) {
        return shopOrderMapper.selectShopOrderStatistics(tenantCode);
    }

    @Override
    public ShopOrderResultVo selectShopOrderInfo(Long shopOrderId) {
        return shopOrderMapper.selectShopOrderInfo(shopOrderId);
    }

    @Override
    public TResponse updateShopOrderInfo(Long shopOrderId, String shippingNo, String deliverExpress, TenantUser tenantUser) {
        ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(shopOrderId);
        if (shopOrder != null) {
            ShopOrderGoodsExample example = new ShopOrderGoodsExample();
            example.createCriteria().andShopOrderIdEqualTo(shopOrderId);
            ShopOrderGoods shopOrderGoods = shopOrderGoodsMapper.selectByExample(example).get(0);

            //1,获取下单数量
            Integer goodsNum = shopOrderGoods.getGoodsNum();

            //2，修改商品库存数量
            ShopGoods shopGoods = shopGoodsMapper.selectByPrimaryKey(shopOrderGoods.getGoodsId());
            Integer stockNum=shopGoods.getStockNum();
            shopGoods.setStockNum(stockNum- goodsNum);
            shopGoods.setStockLockedCount(shopGoods.getStockLockedCount() - goodsNum);
            shopGoods.setExchangeNum(shopGoods.getExchangeNum() + goodsNum);
            shopGoods.setLastUpdatedBy(tenantUser.getLoginName());
            shopGoods.setLastUpdatedTime(new Date());
            shopGoodsMapper.updateByPrimaryKeySelective(shopGoods);

            //3,新增商品库存变动记录(订单发货)
            ShopStockRecord shopStockRecord = new ShopStockRecord();
            shopStockRecord.setTenantCode(tenantUser.getTenantCode());
            //库存变动类型（0，订单发货；1，订单退货；2，后台减货；3，后台进货）
            shopStockRecord.setStockRecordType("0");
            shopStockRecord.setShopOrderId(shopOrderId);
            shopStockRecord.setShopGoodsId(shopGoods.getGoodsId());
            shopStockRecord.setVariation(goodsNum);
            //商品库存变动后数量
            shopStockRecord.setStockNum(shopGoods.getStockNum());
            shopStockRecord.setShippingNo(shopOrder.getShopOrderNum());
            shopStockRecord.setRemark("订单发货");
            shopStockRecord.setCreatedBy(tenantUser.getLoginName());
            shopStockRecord.setCreatedTime(new Date());
            shopStockRecordMapper.insertSelective(shopStockRecord);

            //4，修改shop_order记录（新增发货物流单号，发货快递公司，发货时间，修改订单状态）
            shopOrder.setShippingNo(shippingNo);
            shopOrder.setDeliverExpress(deliverExpress);
            shopOrder.setStatus("2");
            shopOrder.setDeliverTime(new Date());
            shopOrder.setLastUpdatedTime(new Date());
            this.updateByPrimaryKeySelective(shopOrder);
            return TResponse.SUCCESS();
        } else {
            return TResponse.FAIL("订单不存在");
        }
    }
}
